package com.hongpu.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hongpu.domain.InvoiceTaxRate;
import com.hongpu.dto.InvoiceTaxRatePageDto;
import com.hongpu.service.InvoiceTaxRateService;
import com.hongpu.utils.ResultDto;
import com.hongpu.vo.InvoiceTaxRateVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 张玉胜
 * @since 2025年05月19日
 */
@RestController
@RequestMapping("/invoiceTaxRate")
public class InvoiceTaxRateController {

    @Autowired
    InvoiceTaxRateService invoiceTaxRateService;

    @PostMapping("/add")
    public ResultDto add(@RequestBody InvoiceTaxRate invoiceTaxRate) {
        boolean save = invoiceTaxRateService.save(invoiceTaxRate);
        if(save){
            return ResultDto.ok();
        }else {
            return ResultDto.fail();
        }
    }
    @GetMapping("/delete/{id}")
    public ResultDto delete(@PathVariable("id") Long id) {
        boolean delete = invoiceTaxRateService.removeById(id);
        if(delete){
            return ResultDto.ok();
        }else {
            return ResultDto.fail();
        }
    }
    @PostMapping("/update")
    public ResultDto update(@RequestBody InvoiceTaxRate invoiceTaxRate) {
        boolean update = invoiceTaxRateService.updateById(invoiceTaxRate);
        if (update){
            return ResultDto.ok();
        }else {
            return ResultDto.fail();
        }
    }
    @PostMapping("/page")
    public ResultDto page(@RequestBody InvoiceTaxRatePageDto invoiceTaxRatePageDto) {
        Long current = invoiceTaxRatePageDto.getCurrent();
        Long size = invoiceTaxRatePageDto.getSize();
        IPage page = new Page(current, size);
        InvoiceTaxRate invoiceTaxRate = new InvoiceTaxRate();
        BeanUtils.copyProperties(invoiceTaxRatePageDto, invoiceTaxRate);
        if(invoiceTaxRate.getDeleted() == null){
            invoiceTaxRate.setDeleted(0);
        }
        IPage<InvoiceTaxRate> taxRateIPage = invoiceTaxRateService.page(page, invoiceTaxRate);
        return ResultDto.ok(taxRateIPage);
    }

    @GetMapping("/list")
    public ResultDto list() {
        List<InvoiceTaxRateVo> list = invoiceTaxRateService.list4Select();
        if(list != null){
            return ResultDto.ok(list);
        }else {
            return ResultDto.fail();
        }
    }


}